//
//  ConClubViewController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/7/13.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//
#import "ConClubViewController.h"
#import <MJRefresh.h>
#import "HomeNetService.h"
#import "BannerModel.h"
#import "MBProgressHUD+MJ.h"

#import "HomeRoolCell.h"
#import "HomeOneSectionViewCell.h"
#import "HomeTwoSectionViewCell.h"
#import "ClubNetwork.h"
#import "ActivityNetService.h"
#import "ConOldActivityDetailController.h"

@interface ConClubViewController ()

@property (nonatomic,assign) NSInteger curPage;
@property (nonatomic,strong) NSMutableArray *banners;
@property (nonatomic,strong) NSArray *urls;
@property (nonatomic,strong) NSMutableArray *sources;

@end

@implementation ConClubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.curPage = 1;
    [self setUpTableView];
    [self getOldActivityList];
    [self getInfoBannerList];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1+self.sources.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return (ScreenW-16)*141/309+5;
    }
    return (ScreenW-16)*125/309+10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        HomeRoolCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeRoolCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.banners = self.banners;
        cell.roolBlock = ^(NSInteger index) {
            ConOldActivityDetailController *detailVc = [[ConOldActivityDetailController alloc]init];
            BannerModel *model = self.banners[index];
            detailVc.ID = [model.ID integerValue];
            detailVc.title = model.title;
            [self.navigationController pushViewController:detailVc animated:YES];
        };
        return cell;
    }
    HomeOneSectionViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeOneSectionViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.isClub = YES;
    cell.model = self.sources[indexPath.row-1];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row>0) {
        ConOldActivityDetailController *detailVc = [[ConOldActivityDetailController alloc]init];
        OldActivityModel *model = self.sources[indexPath.row-1];
        if (model.videoUrl.length > 0) {
            detailVc.videoURL = [NSURL URLWithString:model.videoUrl];
            detailVc.videoName = model.videoName;
            detailVc.avatarUrl = model.avatarUrl;
        }
        detailVc.ID = model.ID;
        detailVc.title = model.title;
        [self.navigationController pushViewController:detailVc animated:YES];
    }
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}
- (void)setUpTableView
{
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.tableView registerClass:[HomeRoolCell class] forCellReuseIdentifier:NSStringFromClass([HomeRoolCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeOneSectionViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([HomeOneSectionViewCell class])];
//    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeTwoSectionViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([HomeTwoSectionViewCell class])];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.curPage = 1;
        [self.sources removeAllObjects];
        [self getOldActivityList];
        [self getInfoBannerList];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.curPage++;
        [self getOldActivityList];
    }];
}
- (void)getOldActivityList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"pageNo"] = @(self.curPage);
    [self showWinWaiter];
    [ClubNetwork getOldActivityListWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWinWaiter];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self hideNoDataLab];
        if (success) {
            self.tableView.mj_footer.hidden = (lists.count < 20);
            if (self.curPage == 1&&lists.count == 0) {
                [self showNoDataLab:self.tableView range:CGRectMake(0, (ScreenH-153)*0.5, ScreenW, 20) title:@"暂无活动"];
                return ;
            }
            [self.sources addObjectsFromArray:lists];
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)getInfoBannerList
{
    [ActivityNetService getInfoBannerListWithParame:nil response:^(BOOL success, NSString *msg,NSArray *banners) {
        if (success) {
            [self.banners removeAllObjects];
            [self.banners addObjectsFromArray:banners];
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (NSMutableArray *)sources
{
    if (!_sources) {
        _sources = [NSMutableArray array];
    }
    return _sources;
}
- (NSMutableArray *)banners
{
    if (!_banners) {
        _banners = [NSMutableArray array];
    }
    return _banners;
}


@end
